package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tandong.bottomview.view.BottomView;
import com.tj.jinzao.R;
import com.tj.jinzao.activity.FamilyPersonInfoActivity;
import com.tj.jinzao.activity.HealthActivity;
import com.tj.jinzao.adapter.FamilyListItemAdapter;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.callback.IFamilyView;
import com.tj.jinzao.model.FamilyMember;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.presenter.FamilyPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.EmptyView;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 家庭成员列表fragment
 * Created by wjy on 2019/1/1.
 */
public class FamilyListFragment extends BaseDataListFragment<List<FamilyMember>, FamilyPresenter, FamilyMember> implements IFamilyView<List<FamilyMember>> {

    //0成员管理，1成员报告
    private int mType = 0;

    public static FamilyListFragment newInstance(int type) {
        FamilyListFragment fragment = new FamilyListFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(type));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.base_fragment_item_layout);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }

    @Override
    protected FamilyPresenter createPresenter() {
        return new FamilyPresenter(this);
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
        if (TextUtils.equals("0", mTag)) {
            View headView = inflater.inflate(R.layout.head_member, null);
            headView.findViewById(R.id.btn_add).setOnClickListener(this);
            headView.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            mListView.addHeaderView(headView);
        }

    }
    private FamilyListItemAdapter familyListItemAdapter;

    @Override
    protected RecyclerView.Adapter createAdapter() {
        familyListItemAdapter=new FamilyListItemAdapter(mContext, mData);
        return familyListItemAdapter;
    }

    @Override
    protected void requestData() {
        mPresenter.getFamilyList();
    }

    @Override
    protected void setEmptyDataView() {
        if (!TextUtils.equals("0", mTag)) {
            mEmptyView.setVisibility(View.VISIBLE);
            mEmptyView.setState(EmptyView.State.NO_DATA, "没有内容哦");
        }
    }

    @Override
    protected boolean resolveData(List<FamilyMember> familyMembers) {
        return false;
    }

    @Override
    public void onRequestSuccess(List<FamilyMember> list) {
        super.onRequestSuccess(list);
        if (mPageNo <= 1) {
            mData.clear();
        }
        mData.addAll(list);
        showListView();

    }

    @Override
    protected boolean canLoadMoreItems() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData(true, false);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.btn_add) {
            FamilyPersonInfoActivity.start(mContext, 0);
        }
    }

    @Override
    protected void initData() {
        familyListItemAdapter.setOnLongLinstener(new FamilyListItemAdapter.OnLongLinstener() {
            @Override
            public void onLongClick(int id) {
                showPopWindowImage(id);
            }
        });
    }
    private BottomView bottomView;
    private void showPopWindowImage(int id){
        bottomView=new BottomView(getActivity(), com.netease.nim.uikit.R.style.BottomViewTheme_Defalut, com.netease.nim.uikit.R.layout.item_select);
        bottomView.setAnimation(com.netease.nim.uikit.R.style.myStyle);
        bottomView.dismissBottomView();
        bottomView.showBottomView(false);
        TextView txt_1= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_1);
        TextView txt_2= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_2);
        View view_line=bottomView.getView().findViewById(com.netease.nim.uikit.R.id.view_line);
        view_line.setVisibility(View.GONE);
        txt_1.setText("删除成员");
        txt_2.setVisibility(View.GONE);
        TextView txt_exit= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_exit);
        txt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteMember(id);
                mPresenter.getFamilyList();
                bottomView.dismissBottomView();

            }
        });
        txt_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomView.dismissBottomView();
            }
        });

    }
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private CompositeSubscription mCompositeSubscription;


    public void detachView() {
        onUnsubscribe();
    }
    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        detachView();
    }

    private void deleteMember(int id){
        addSubscription(mApiService.deleteMember(id), new SubscriberCallBack<FamilyMember>(getActivity()) {
            @Override
            protected void onSuccess(FamilyMember list) {
                ToastUtils.showToast(getActivity(),"删除成功");
            }

            @Override
            protected void onFailure(ResultResponse response) {

            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }

            @Override
            protected void onPageSuccess(int currentPage, int allPage, int allSize) {
            }
        });

    }

    @Override
    protected void itemClicked(int position, FamilyMember data) {
        if (TextUtils.equals("0", mTag)) {
            FamilyPersonInfoActivity.start(mContext, data.getId());
        } else {
            mContext.startActivity(new Intent(mContext, HealthActivity.class)
                    .putExtra("id",data.getId())
                    .putExtra("name", data.getName())
                    .putExtra("mFamilyId",data.getId())
                    .putExtra("userId", String.valueOf(data.getUid())));
        }
    }

    @Override
    public void onAddFamily(FamilyMember member) {

    }

    @Override
    public void onUploadSuccess(String path, String url) {

    }
}
